#include "CParticles.hpp"

CParticles::CParticles()
{
	mBox=NULL;
	mNumParticles=0;
	mNumSectioned=0;
	mZHeight=0;
}

CParticles::~CParticles()
{
	while (mBox!=NULL)
	{
		CParticle* temp;
		temp=mBox;
		mBox=temp->GetNext();
		delete temp;
	}
}

void CParticles::SetZHeight(double zHeight)
{
	mZHeight=zHeight;
}

double CParticles::GetZHeight()
{
	return mZHeight;
}

CParticle* CParticles::NewParticle(double X, double Y, double Z, double R)
{
	CParticle* particle;
	particle= new CParticle(X, Y, Z, R);
	return particle;
}

void CParticles::Insert(CParticle* particle)
{
	CParticle* next;
	next=mBox;
	if (next==NULL)
		mBox=particle;
	else
	{
		particle->SetNext(next);
		mBox=particle;
	}
}

void CParticles::Delete(CParticle* particle)
{
	CParticle* next;
	CParticle* previous;
	
	if (mBox==particle)
		mBox=particle->GetNext();
	else	
	{
		previous=mBox;
		next=previous->GetNext();
		
		while ((next!=NULL)&&(next!=particle))
		{
			previous=next;
			next=previous->GetNext();

		}

		if (next==particle)
		{
			previous->SetNext(next->GetNext());
			mNumParticles--;
		}
	}
	
	delete particle;
}

CParticle* CParticles::ReadData()
{
	using namespace std;
	
	char fileName[256];
	string readBuffer;
	ifstream dataFile;
	CParticle* temp;
	double x, y, z, r;
	
	cout<<"Input the file name made from the SortCube: ";
	cin>>fileName;
	cout<<endl;
	
	dataFile.open(fileName);

	getline(dataFile, readBuffer);
	istringstream particle(readBuffer);
	
	while (!dataFile.eof())
	{
		istringstream particle(readBuffer);
		particle>>x>>y>>z>>r;
		temp=NewParticle(x,y,z,r);
		Insert(temp);
		mNumParticles++;
		getline(dataFile, readBuffer);
	}

#ifdef DEBUG
		temp=mBox;
	int ii=1;
	while (temp!=NULL)
	{
		cout<<ii<<"\t"<<temp->GetR()<<endl;
		temp=temp->GetNext();
		ii++;		
	}
#endif

	cout<<"Total number of particles: "<<mNumParticles<<endl;

	return mBox;
}

void CParticles::Sectioning()
{
	using namespace std;

	double zHeight=0;
	double z=0;
	double r=0;
	double zMax=0;
	double zMin=0;
	double deltaZ=0;
	double theta=0;
	CParticle* indicator;
	
	cout<<endl<<"Input the height(Z-value) for sectioning: ";
	cin>>zHeight;
	cout<<endl;
	SetZHeight(zHeight);
	
	indicator=mBox;
	
	while (indicator!=NULL)
	{
		z=indicator->GetZ();
		r=indicator->GetR();
		zMax=z+r;
		zMin=z-r;
		if ((zHeight<zMax)&&(zHeight>zMin))
		{
			indicator->SetSect(true);
			mNumSectioned++;
			deltaZ=fabs(z-zHeight);
			theta=acos(deltaZ/r);
			indicator->SetSectR(deltaZ*tan(theta));
		}
	
		indicator=indicator->GetNext();
	}
	
	cout<<"The number of particles sectioned: "<<mNumSectioned<<endl;
}

void CParticles::PrintSection()
{
	using namespace std;

	CParticle* indicator;
	
	ofstream file;
	file.open("2D-section.dat");
	
	indicator=mBox;
	
	while (indicator!=NULL)
	{
		if (indicator->GetSect())
		{
			file<<indicator->GetX()
				<<"\t"
				<<indicator->GetY()
				<<"\t"
				<<indicator->GetSectR()
				<<endl;
		}		
		indicator=indicator->GetNext();
	}

	file.close();

#ifdef DEBUG
		indicator=mBox;
	int ii=1;
	while (indicator!=NULL)
	{
		if (indicator->GetSect())
		{
			cout<<ii<<"\t"<<indicator->GetSectR()<<endl;
			ii++;
		}		
		indicator=indicator->GetNext();
	}
#endif
}

